﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using BulkCopy;
using BulkCopy.Internals;
using GBase.Data.GBaseClient;

namespace Browis.Cds.Plugin
{

    public class NMGCommand
    {
        public static IList<string> NMGPakUpdateCommandNames;

        public static IList<string> NMGPakBulkCommandNames;

        static NMGCommand()
        {
            NMGPakUpdateCommandNames = new List<string>();
            NMGPakUpdateCommandNames.Add("MODIFY_tbArea");
            NMGPakUpdateCommandNames.Add("MODIFY_tbUnit");
            NMGPakUpdateCommandNames.Add("MODIFY_tbData_New");
            NMGPakUpdateCommandNames.Add("MODIFY_tbData_NewOrder");
            NMGPakUpdateCommandNames.Add("MODIFY_tbData_Old");
            NMGPakUpdateCommandNames.Add("MODIFY_tbData");
            NMGPakUpdateCommandNames.Add("MODIFY_tbSequence_New");
            NMGPakUpdateCommandNames.Add("MODIFY_tbSequence_NewOrder");
            NMGPakUpdateCommandNames.Add("MODIFY_tbSequence_Old");
            NMGPakUpdateCommandNames.Add("MODIFY_tbSequence");
            NMGPakUpdateCommandNames.Add("MODIFY_tbSequenceWithSequences");
            NMGPakUpdateCommandNames.Add("tbSequenceWithSequences");
            NMGPakUpdateCommandNames.Add("INSERT_INTO_Temp_FROM_tbProductTree");
            NMGPakUpdateCommandNames.Add("INSERT_INTO_Temp_FROM_tbProductTree_User");
            NMGPakUpdateCommandNames.Add("INITIALIZER_tbProductTree_TEMP");
            NMGPakUpdateCommandNames.Add("INITIALIZER_tbProductTree_TEMP_User");
            NMGPakUpdateCommandNames.Add("INSERT_tbProductTree_TEMP");
            NMGPakUpdateCommandNames.Add("INSERT_tbProductTree_TEMP_User");
            NMGPakUpdateCommandNames.Add("DELETE_tbProductTree");
            NMGPakUpdateCommandNames.Add("INSERT_INTO_tbProductTree_FROM_Temp");
            NMGPakUpdateCommandNames.Add("INSERT_INTO_tbProductTree_FROM_Temp_User");
            NMGPakUpdateCommandNames.Add("DELETE_ProductTree_Structure");
            NMGPakUpdateCommandNames.Add("DELETE_ProductTree_Structure_User");
            NMGPakUpdateCommandNames.Add("DROP_tbProductTreeTemp");
            NMGPakUpdateCommandNames.Add("DROP_tbProductTreeTemp_User");
            NMGPakUpdateCommandNames.Add("UPDATE_tbDataWithInvalidState");
            NMGPakUpdateCommandNames.Add("UPDATE_tbSequenceWithInvalidState");
            NMGPakUpdateCommandNames.Add("TRUNCATE_DBTABLES");
            
            //序列更新
            NMGPakUpdateCommandNames.Add("UPDATE_tbSequencesfdNewDataUpdateTime");

            ///手动全包命令
            NMGPakUpdateCommandNames.Add("MODIFY_tbProductTree");

            NMGPakBulkCommandNames = new List<string>();
            NMGPakBulkCommandNames.Add("BULK_tbData");
            NMGPakBulkCommandNames.Add("BULK_tbSequence");
            NMGPakBulkCommandNames.Add("BULK_tbSequenceWithSequences");
            NMGPakBulkCommandNames.Add("BULK_tbProductTree");
        }

        public static bool CheckNMGPakUpdateCommand(string pakCommandName)
        {
            if (string.IsNullOrEmpty(pakCommandName))
            {
                return false;
            }
            if (NMGPakUpdateCommandNames.Contains(pakCommandName))
            {
                return true;
            }
            return false;
        }

        public static bool CheckNMGPakBulkCommand(string pakCommandName)
        {
            if (string.IsNullOrEmpty(pakCommandName))
            {
                return false;
            }
            if (NMGPakBulkCommandNames.Contains(pakCommandName))
            {
                return true;
            }
            return false;
        }

        //public static string GetCommandText(string pakCommandName,string releaseText)
        public static void GetCommentTextAndType(IDbCommand command, string pakCommandName, string releaseText)
        {
            if (command == null || string.IsNullOrEmpty(pakCommandName))
            {
                return;
            }

            string cmdText = string.Empty;
            CommandType cType = CommandType.Text;

            if (string.Compare(pakCommandName, "MODIFY_tbArea", true) == 0)
            {
                cmdText = @"DELETE FROM tbArea WHERE fdCode=?fdCode OR fdName=?fdName;
                        INSERT INTO tbArea (fdCode,fdName,fdAlias,fdState,fdUpdateTime) 
                        VALUES(?fdCode,?fdName,?fdAlias,?fdState,?fdUpdateTime);";
            }
            else if (string.Compare(pakCommandName, "MODIFY_tbData_New", true) == 0 ||
                string.Compare(pakCommandName, "MODIFY_tbData_NewOrder", true) == 0 ||
                string.Compare(pakCommandName, "MODIFY_tbData_Old", true) == 0 ||
                string.Compare(pakCommandName, "MODIFY_tbData", true) == 0)
            {
                cType = CommandType.StoredProcedure;
                cmdText = "MODIFY_tbData";
                /*cmdText = @"IF EXISTS(SELECT fdData FROM tbData WHERE fdSequenceID=?fdSequenceID AND fdInnerTime=?fdInnerTime)
                          BEGIN
                            UPDATE tbData SET fdTime=?fdTime,fdData=?fdData,fdState=?fdState,fdUpdateTime=now()
                            WHERE fdSequenceID=?fdSequenceID AND fdInnerTime=?fdInnerTime 
                          END
                        ELSE
                          BEGIN
                            INSERT INTO tbData (fdSequenceID,fdTime,fdInnerTime,fdData,fdUpdateTime,fdState) 
                            VALUES(?fdSequenceID,?fdTime,?fdInnerTime,?fdData,now(),?fdState)
                          END";*/
            }
            else if (string.Compare(pakCommandName, "MODIFY_tbSequence_New", true) == 0 ||
                string.Compare(pakCommandName, "MODIFY_tbSequence_NewOrder", true) == 0 ||
                string.Compare(pakCommandName, "MODIFY_tbSequence_Old", true) == 0 ||
                string.Compare(pakCommandName, "MODIFY_tbSequence", true) == 0)
            {
                cType = CommandType.StoredProcedure;
                cmdText = "MODIFY_tbSequence";
                /*cmdText = @"IF EXISTS(SELECT fdID FROM tbSequence WHERE fdID=?fdID)
                          BEGIN
                            UPDATE tbSequence 
                            SET 
                            fdIndicatorCode=?fdIndicatorCode + '：ZZZZ' + UPPER(?fdDatabaseCode),
                            fdDisplayName=?fdDisplayName,
                            fdAreaCode=?fdAreaCode,
                            fdFrequencyCode=?fdFrequencyCode,
                            fdUnit=?fdUnit,
                            fdStartTime=?fdStartTime,
                            fdEndTime=?fdEndTime,
                            fdAmountOfData=?fdAmountOfData,
                            fdSource=?fdSource,
                            fdState=?fdState,
                            fdAnnotation=?fdAnnotation,
                            fdUpdateID='CEI',
                            fdUpdateName='CEI',
                            fdUpdateTime=now(),
                            fdDataUpdateTime=?fdDataUpdateTime,
                            fdNewDataUpdateTime=?fdNewDataUpdateTime                             
                            WHERE fdID=?fdID
                          END
                        ELSE
                          BEGIN
                            INSERT INTO tbSequence (fdID,fdIndicatorCode,fdDisplayName,fdAreaCode,fdFrequencyCode,fdUnit,fdStartTime,fdEndTime,fdAmountOfData,fdSource,fdState,fdAnnotation,fdUpdateID,fdUpdateName,fdUpdateTime,fdDataUpdateTime,fdNewDataUpdateTime) 
                            VALUES(?fdID,?fdIndicatorCode + '：ZZZZ' + UPPER(?fdDatabaseCode),?fdDisplayName,?fdAreaCode,?fdFrequencyCode,?fdUnit,?fdStartTime,?fdEndTime,?fdAmountOfData,?fdSource,?fdState,?fdAnnotation,'CEI','CEI',now(),?fdDataUpdateTime,?fdNewDataUpdateTime)
                          END;
                        IF EXISTS(SELECT fdCode FROM tbIndicator WHERE fdCode=?fdIndicatorCode + '：ZZZZ' + UPPER(?fdDatabaseCode))
                          BEGIN
                            UPDATE tbIndicator 
                            SET 
                            fdName=?fdDisplayName,
                            fdAlias=?fdIndicatorAlias,
                            fdUpdateTime=now()
                            WHERE fdCode=?fdIndicatorCode + '：ZZZZ' + UPPER(?fdDatabaseCode)
                          END
                        ELSE
                          BEGIN
                            INSERT INTO tbIndicator (fdCode,fdName,fdAlias,fdUpdateTime) 
                            VALUES(?fdIndicatorCode + '：ZZZZ' + UPPER(?fdDatabaseCode),?fdDisplayName,?fdIndicatorAlias,now())
                          END";*/
            }
            else if (string.Compare(pakCommandName, "MODIFY_tbSequenceWithSequences", true) == 0)
            {
                cType = CommandType.StoredProcedure;
                cmdText = "MODIFY_tbSequenceWithSequences";
                /*cmdText = @"IF EXISTS(SELECT fdSequencesID FROM tbSequenceWithSequences WHERE fdSequenceID=@fdSequenceID AND fdSequencesID=@fdSequencesID)
                      BEGIN
                        UPDATE tbSequenceWithSequences SET fdXIndex=@fdXIndex WHERE fdSequenceID=@fdSequenceID AND fdSequencesID=@fdSequencesID
                      END
                    ELSE
                      BEGIN
                        INSERT INTO tbSequenceWithSequences (fdSequenceID,fdSequencesID,fdXIndex) 
                        VALUES(@fdSequenceID,@fdSequencesID,@fdXIndex);
                      END";*/
            }
            else if (string.Compare(pakCommandName, "INITIALIZER_tbProductTree_TEMP", true) == 0)
            {
                cType = CommandType.StoredProcedure;
                cmdText = "INITIALIZER_tbProductTree_TEMP";
                /*cmdText = @"IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE name = 'tbTreeTemp' AND type = 'U') 
                BEGIN                   
                  CREATE TABLE tbTreeTemp (
                     fdNodeID             uniqueidentifier      NOT NULL ,
                     fdSyncID             int                   NOT NULL ,
                     fdNodePath           varchar(512)          NOT NULL ,
                     fdParentID           uniqueidentifier      NULL,
                     fdType				  int                   NOT NULL,
                     fdName               nvarchar(128)         NULL,
                     fdXIndex             int                   NOT  NULL,
                     fdUpdateID           varchar(32)           NOT NULL,
	                 fdUpdateName         nvarchar(32)          NOT NULL,
                     fdUpdateTime         datetime              NOT  NULL,
					            PRIMARY KEY CLUSTERED 
					            (
						            fdNodeID ASC,
						            fdSyncID ASC
					            )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
					            ) ON [PRIMARY]
                END;
              ELSE
                BEGIN
                  TRUNCATE TABLE tbTreeTemp;
                END";*/
            }
            else if (string.Compare(pakCommandName, "INITIALIZER_tbProductTree_TEMP_User", true) == 0)
            {
                cType = CommandType.StoredProcedure;
                cmdText = "INITIALIZER_tbProductTree_TEMP_User";
                /*cmdText = @"IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE name = 'tbTreeTemp_User' AND type = 'U') 
                BEGIN                   
                  CREATE TABLE tbTreeTemp_User (
                     fdNodeID             uniqueidentifier      NOT NULL ,
                     fdSyncID             int                   NOT NULL ,
                     fdNodePath           varchar(512)          NOT NULL ,
                     fdParentID           uniqueidentifier      NULL,
                     fdType				  int                   NOT NULL,
                     fdName               nvarchar(128)         NULL,
                     fdXIndex             int                   NOT  NULL,
                     fdUpdateID           varchar(32)           NOT NULL,
	                 fdUpdateName         nvarchar(32)          NOT NULL,
                     fdUpdateTime         datetime              NOT  NULL,
					            PRIMARY KEY CLUSTERED 
					            (
						            fdNodeID ASC,
						            fdSyncID ASC
					            )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
					            ) ON [PRIMARY]
                END;
              ELSE
                BEGIN
                  TRUNCATE TABLE tbTreeTemp_User;
                END";*/ 
            }
            else if (string.Compare(pakCommandName, "INSERT_INTO_Temp_FROM_tbProductTree", true) == 0)
            {
                cmdText = @"INSERT INTO tbTreeTemp(fdNodeID,fdSyncID,fdNodePath,fdParentID,fdType,fdName,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime) 
                            SELECT fdNodeID,2,fdNodePath,fdParentID,fdType,fdName,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime FROM tbTree 
                            WHERE (fdNodePath LIKE CONCAT('%', CAST(?fdNodeID AS CHAR(64)), '%'));";
            }
            else if (string.Compare(pakCommandName, "INSERT_INTO_Temp_FROM_tbProductTree_User", true) == 0)
            {
                cmdText = @"INSERT INTO tbTreeTemp_User(fdNodeID,fdSyncID,fdNodePath,fdParentID,fdType,fdName,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime) 
                            SELECT fdNodeID,2,fdNodePath,fdParentID,fdType,fdName,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime FROM tbTree 
                            WHERE (fdNodePath LIKE CONCAT('%', CAST(?fdNodeID AS CHAR(64)), '%'));";
            }
            else if (string.Compare(pakCommandName, "INSERT_tbProductTree_TEMP", true) == 0)
            {
                cmdText = @"INSERT INTO tbTreeTemp (fdNodeID,fdSyncID, fdNodePath,fdParentID,fdType,fdName,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime) 
                VALUES(?fdNodeID,1,?fdNodePath,?fdParentID,?fdType,?fdName,?fdXIndex,'CEI','CEI',now())";
            }
            else if (string.Compare(pakCommandName, "INSERT_tbProductTree_TEMP_User", true) == 0)
            {
                cmdText = @"INSERT INTO tbTreeTemp_User (fdNodeID,fdSyncID, fdNodePath,fdParentID,fdType,fdName,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime) 
                VALUES(?fdNodeID,1,?fdNodePath,?fdParentID,?fdType,?fdName,?fdXIndex,'CEI','CEI',now())";
            }
            else if (string.Compare(pakCommandName, "DELETE_tbProductTree", true) == 0)
            {
                cmdText = @"DELETE FROM tbTree WHERE fdNodePath LIKE CONCAT('%', CAST(?fdNodeID AS CHAR(64)), '%');";
            }
            else if (string.Compare(pakCommandName, "INSERT_INTO_tbProductTree_FROM_Temp", true) == 0)
            {
                cmdText = @"INSERT INTO tbTree(fdNodeID,fdNodePath,fdParentID,fdType,fdName,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime) 
                            SELECT fdNodeID,fdNodePath,fdParentID,fdType,fdName,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime FROM tbTreeTemp WHERE fdSyncID=1;";
            }
            else if (string.Compare(pakCommandName, "INSERT_INTO_tbProductTree_FROM_Temp_User", true) == 0)
            {
                cmdText = @"INSERT INTO tbTree(fdNodeID,fdNodePath,fdParentID,fdType,fdName,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime) 
                            SELECT fdNodeID,fdNodePath,fdParentID,fdType,fdName,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime FROM tbTreeTemp_User WHERE fdSyncID=1;";
            }
            else if (string.Compare(pakCommandName, "DELETE_ProductTree_Structure", true) == 0)
            {
                cmdText = @"DELETE FROM tbSequenceWithSequences WHERE fdSequencesID IN 
                 ( 
                    SELECT DISTINCT fdNodeID FROM tbTreeTemp WHERE fdNodePath LIKE CONCAT('%', CAST(?fdNodeID AS CHAR(64)), '%') AND fdType=1 AND fdSyncID=2 AND 
                    fdNodeID NOT IN
                    (
                      SELECT fdNodeID FROM tbTreeTemp WHERE fdNodePath LIKE CONCAT('%', CAST(?fdNodeID AS CHAR(64)), '%') AND fdType=1 AND fdSyncID=1
                    )
                  );";
            }
            else if (string.Compare(pakCommandName, "DELETE_ProductTree_Structure_User", true) == 0)
            {
                cmdText = @"DELETE FROM tbSequenceWithSequences WHERE fdSequencesID IN 
                 ( 
                    SELECT DISTINCT fdNodeID FROM tbTreeTemp_User WHERE fdNodePath LIKE CONCAT('%', CAST(?fdNodeID AS CHAR(64)), '%') AND fdType=1 AND fdSyncID=2 AND 
                    fdNodeID NOT IN
                    (
                      SELECT fdNodeID FROM tbTreeTemp_User WHERE fdNodePath LIKE CONCAT('%', CAST(?fdNodeID AS CHAR(64)), '%') AND fdType=1 AND fdSyncID=1
                    )
                  );";
            }
            else if (string.Compare(pakCommandName, "DROP_tbProductTreeTemp", true) == 0)
            {
                cmdText = @"DROP TABLE tbTreeTemp;";
            }
            else if (string.Compare(pakCommandName, "DROP_tbProductTreeTemp_User", true) == 0)
            {
                cmdText = @"DROP TABLE tbTreeTemp_User;";
            }
            else if (string.Compare(pakCommandName, "UPDATE_tbDataWithInvalidState", true) == 0)
            {
                cmdText = @"UPDATE tbData SET fdState=3,fdUpdateTime=now() WHERE fdSequenceID NOT IN 
                      (
                      SELECT fdSequenceID FROM tbSequenceWithSequences 
                      )";
            }
            else if (string.Compare(pakCommandName, "UPDATE_tbSequenceWithInvalidState", true) == 0)
            {
                cmdText = @"UPDATE tbSequence SET fdState=3,fdUpdateTime=now() WHERE fdID NOT IN 
              (SELECT fdSequenceID FROM tbSequenceWithSequences)";
            }
            else if (string.Compare(pakCommandName, "MODIFY_tbUnit", true) == 0)
            {
                cType = CommandType.StoredProcedure;
                cmdText = "MODIFY_tbUnit";
            }
            else if (string.Compare(pakCommandName, "TRUNCATE_DBTABLES", true) == 0)
            {
                cmdText = @"TRUNCATE TABLE tbArea;
                         TRUNCATE TABLE tbData;
                         TRUNCATE TABLE tbUnit;
                         TRUNCATE TABLE tbTree;
                         TRUNCATE TABLE tbSequence;
                         TRUNCATE TABLE tbSequenceWithSequences;
                         TRUNCATE TABLE tbIndicator";
            }
            else if (string.Compare(pakCommandName, "MODIFY_tbProductTree", true) == 0)
            {
                cmdText = @"INSERT INTO tbTree (fdNodeID,fdNodePath,fdParentID,fdType,fdName,fdXIndex,fdUpdateID,fdUpdateName,fdUpdateTime) 
                VALUES(?fdNodeID,?fdNodePath,?fdParentID,?fdType,?fdName,?fdXIndex,'CEI','CEI',now())";
            }
			else if (string.Compare(pakCommandName, "UPDATE_tbSequencesfdNewDataUpdateTime", true) == 0)
            {
                if (releaseText.Contains("tbProductTree"))
                {
                    cmdText = @"UPDATE tbSequence SET fdAmountOfDataReal = (SELECT COUNT(*) FROM tbData D WHERE D.fdSequenceID= fdID) WHERE fdID IN
                                  (SELECT fdSequenceID FROM tbSequenceWithSequences WHERE fdSequencesID IN 
	                                (SELECT fdNodeID FROM tbTree WHERE (fdNodePath LIKE CONCAT('%', CAST(?ProductNodeID AS CHAR(64)), '%')) AND fdType=1)
                                  )";
                }
                else
                {
                    cmdText = @"UPDATE tbSequence SET fdAmountOfDataReal = (SELECT COUNT(*) FROM tbData D WHERE D.fdSequenceID= fdID) WHERE fdID=?fdID";
                }
                
            }
            command.CommandText = cmdText;
            command.CommandType = cType;
        }

        public static string GetBulkDestinationTableName(string pakCommandName)
        {
            if (string.IsNullOrEmpty(pakCommandName))
            {
                throw new ArgumentNullException("pakCommandName");
            }
            string strTableName = string.Empty;
            if (string.Compare(pakCommandName, "BULK_tbData", true) == 0)
            {
                strTableName = "tbData";
            }
            else if (string.Compare(pakCommandName, "BULK_tbSequence", true) == 0)
            {
                strTableName = "tbSequence";
            }
            else if (string.Compare(pakCommandName, "BULK_tbSequenceWithSequences", true) == 0)
            {
                strTableName = "tbSequenceWithSequences";
            }
            else if (string.Compare(pakCommandName, "BULK_tbProductTree", true) == 0)
            {
                strTableName = "tbTree";
            }
            return strTableName;
        }

        public static DataTable GetDataTable(GBaseBulkCopy nMGBulkCopy, DataTable sourceDT, string pakCommandName, ref IDictionary<string, DataRow> refIndicator)
        {
            DataTable resultDT = sourceDT;
            if (string.Compare(pakCommandName, "BULK_tbData",true) == 0)
            {
                resultDT.Columns.Remove("fdUpdateTime");

                nMGBulkCopy.ColumnMappings.Clear();
                nMGBulkCopy.ColumnMappings.Add("fdSequenceID", "fdSequenceID", DataType.Number);
                nMGBulkCopy.ColumnMappings.Add("fdInnerTime", "fdInnerTime", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdTime", "fdTime", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdData", "fdData", DataType.Number);
                nMGBulkCopy.ColumnMappings.Add("fdState", "fdState", DataType.Number);

            }
            else if (string.Compare(pakCommandName, "BULK_tbSequence", true) == 0)
            {
                resultDT.Columns.Add(new DataColumn("fdUpdateID", typeof(string)));
                resultDT.Columns.Add(new DataColumn("fdUpdateName", typeof(string)));

                refIndicator = new Dictionary<string, DataRow>();

                DataTable dtIndicator = new DataTable();
                dtIndicator.Columns.Add(new DataColumn("fdIndicatorCode", typeof(string)));
                dtIndicator.Columns.Add(new DataColumn("fdDisplayName", typeof(string)));
                dtIndicator.Columns.Add(new DataColumn("fdIndicatorAlias", typeof(string)));
                dtIndicator.Columns.Add(new DataColumn("fdAmountOfDataReal", typeof(int)));


                foreach (DataRow row in resultDT.Rows)
                {
                    row["fdUpdateID"] = "CEI";
                    row["fdUpdateName"] = "CEI";
                    row["fdIndicatorCode"] = row["fdIndicatorCode"] + "：ZZZZ" + row["fdDatabaseCode"].ToString().ToUpper();

                    if (!refIndicator.Keys.Contains(row["fdIndicatorCode"].ToString()))
                    {
                        DataRow dr = dtIndicator.NewRow();
                        dr["fdIndicatorCode"] = row["fdIndicatorCode"];
                        dr["fdDisplayName"] = row["fdDisplayName"];
                        dr["fdIndicatorAlias"] = row["fdIndicatorAlias"];
                        dr["fdAmountOfDataReal"] = 0;

                        refIndicator.Add(row["fdIndicatorCode"].ToString(), dr);
                    }

                }

                resultDT.Columns.Remove("fdIndicatorAlias");
                resultDT.Columns.Remove("fdIsCalculate");
                resultDT.Columns.Remove("fdDatabaseCode");
                resultDT.Columns.Remove("fdUpdateTime");

                nMGBulkCopy.ColumnMappings.Clear();
                nMGBulkCopy.ColumnMappings.Add("fdID", "fdID", DataType.Number);
                nMGBulkCopy.ColumnMappings.Add("fdIndicatorCode", "fdIndicatorCode", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdDisplayName", "fdDisplayName", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdAreaCode", "fdAreaCode", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdFrequencyCode", "fdFrequencyCode", DataType.Number);

                nMGBulkCopy.ColumnMappings.Add("fdUnit", "fdUnit", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdStartTime", "fdStartTime", DataType.DateTime);
                nMGBulkCopy.ColumnMappings.Add("fdEndTime", "fdEndTime", DataType.DateTime);
                nMGBulkCopy.ColumnMappings.Add("fdAmountOfData", "fdAmountOfData", DataType.Number);
                nMGBulkCopy.ColumnMappings.Add("fdSource", "fdSource", DataType.Text);

                nMGBulkCopy.ColumnMappings.Add("fdState", "fdState", DataType.Number);
                nMGBulkCopy.ColumnMappings.Add("fdAnnotation", "fdAnnotation", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdUpdateID", "fdUpdateID", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdUpdateName", "fdUpdateName", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdDataUpdateTime", "fdDataUpdateTime", DataType.DateTime);

                nMGBulkCopy.ColumnMappings.Add("fdNewDataUpdateTime", "fdNewDataUpdateTime", DataType.DateTime);
               
            }
            else if (string.Compare(pakCommandName, "BULK_tbProductTree", true) == 0)
            {
                resultDT.Columns.Add(new DataColumn("fdUpdateID", typeof(string)));
                resultDT.Columns.Add(new DataColumn("fdUpdateName", typeof(string)));
                foreach (DataRow row in resultDT.Rows)
                {
                    row["fdUpdateID"] = "CEI";
                    row["fdUpdateName"] = "CEI";
                }

                resultDT.Columns.Remove("fdUpdateTime");
                nMGBulkCopy.ColumnMappings.Clear();
                nMGBulkCopy.ColumnMappings.Add("fdNodeID", "fdNodeID", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdNodePath", "fdNodePath", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdParentID", "fdParentID", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdType", "fdType", DataType.Number);
                nMGBulkCopy.ColumnMappings.Add("fdName", "fdName", DataType.Text);

                nMGBulkCopy.ColumnMappings.Add("fdXIndex", "fdXIndex", DataType.Number);
                nMGBulkCopy.ColumnMappings.Add("fdUpdateID", "fdUpdateID", DataType.Text);
                nMGBulkCopy.ColumnMappings.Add("fdUpdateName", "fdUpdateName", DataType.Text);
            }
            
            return resultDT;
        }

        public static int UpdateIndicator(IDictionary<string, DataRow> refIndicator)
        {
            int effectedCount = 0;
            using (GBaseConnection conn = new GBaseConnection(ConfigurationManager.ConnectionStrings["内蒙基础库"].ConnectionString))
            {
                conn.Open();
                GBaseCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.CommandText = "UpdateIndicator";

                cmd.Parameters.Add("g_fdIndicatorCode", GBaseDbType.VarChar);
                cmd.Parameters.Add("g_fdDisplayName", GBaseDbType.VarChar);
                cmd.Parameters.Add("g_fdIndicatorAlias", GBaseDbType.VarChar);

                foreach (DataRow row in refIndicator.Values)
                {
                    cmd.Parameters[0].Value = row["fdIndicatorCode"];
                    cmd.Parameters[1].Value = row["fdDisplayName"];
                    cmd.Parameters[2].Value = row["fdIndicatorAlias"];

                    effectedCount +=cmd.ExecuteNonQuery();
                }
            }

            return effectedCount;
        }
    }
}
